package com.wangsanfei.base;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.upload.UploadFile;
import com.wangsanfei.config.ConstConfig;
import com.wangsanfei.model.FileInfo;
import com.wangsanfei.model.User;

public class BaseController extends Controller{

	public HttpSession getSession(){
		return super.getSession();
	}
	
	public FileInfo addFile(String fileParam,String userId){
		String path=PropKit.get("file.storePath");
		UploadFile uploadFile=getFile(fileParam, path);
		if(uploadFile==null){
			return null;
		}
		FileInfo fileInfo=FileInfo.dao.add(uploadFile,userId);
		return fileInfo;
	}
	
	public ArrayList<FileInfo> addFiles(String fileParam,String userId){
		String path=PropKit.get("file.storePath");
		System.out.println(PathKit.getWebRootPath());
		List<UploadFile> ups = getFiles(path);
		ArrayList<FileInfo> list = new ArrayList<FileInfo>();
		for(UploadFile file:ups){
			FileInfo fileInfo=FileInfo.dao.add(file,userId);
			list.add(fileInfo);
		}
		return list;
	}
	
	public UploadFile uploadFile(String fileParam){
		String path=PropKit.get("file.storePath");
		UploadFile uploadFile=getFile(fileParam, path);
		if(uploadFile==null){
			return null;
		}
		return uploadFile;
	}
	
	public User getCurrenUser(){
		return (User)getSession().getAttribute(ConstConfig.SESSION_USER);
	}
}
